﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DotNet.Edu.Entity;
using DotNet.Edu.Service;
using DotNet.Extensions;
using DotNet.Utility;

namespace DotNet.EduWebApi.Controllers
{
    public class UserController : BaseApiController
    {
        /// <summary>
        /// 获取学员基础信息
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        [ApiCheck, HttpPost]
        public ActionResult GetStudentInfo(string userId)
        {
            #region 参数验证解密

            if (userId.IsEmpty())
            {
                return Json(new BoolMessage(false, "请指定学员主键"));
            }

            var validResult = EncryptUtil.Decrypt(userId, "学员主键");
            if (!validResult.Success)
            {
                return Json(validResult);
            }

            userId = validResult.Data;

            #endregion

            var student = GetStudentById(userId);
            return Json(new BoolMessageData(student));
        }

        /// <summary>
        /// 获取学员学时明细
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        [ApiCheck, HttpPost]
        public ActionResult GetPeriodDetails(string userId)
        {
            #region 参数验证解密

            if (userId.IsEmpty())
            {
                return Json(new BoolMessage(false, "请指定学员主键"));
            }

            var validResult = EncryptUtil.Decrypt(userId, "学员主键");
            if (!validResult.Success)
            {
                return Json(validResult);
            }

            userId = validResult.Data;

            #endregion

            #region 验证分页信息

            var pageInfo = GetPageInfo();
            if (!pageInfo.Success)
            {
                return Json(pageInfo);
            }

            #endregion

            var data = EduService.PeriodDetails.GetStudentPageList(pageInfo.Data, userId);
            data.ForEach(p=>p.CoursewareName = EduService.Courseware.GetName(p.CoursewareId));
            return Json(new BoolMessagePageList<PeriodDetails>(data));
        }

        /// <summary>
        /// 获取学员验证记录
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        [ApiCheck, HttpPost]
        public ActionResult GetLearnHistory(string userId)
        {
            #region 参数验证解密

            if (userId.IsEmpty())
            {
                return Json(new BoolMessage(false, "请指定学员主键"));
            }

            var validResult = EncryptUtil.Decrypt(userId, "学员主键");
            if (!validResult.Success)
            {
                return Json(validResult);
            }

            userId = validResult.Data;

            #endregion

            #region 验证分页信息

            var pageInfo = GetPageInfo();
            if (!pageInfo.Success)
            {
                return Json(pageInfo);
            }

            #endregion

            var data = EduService.LessonLog.GetPageList(pageInfo.Data, userId);
            return Json(new BoolMessagePageList<LessonLog>(data));
        }

        /// <summary>
        /// 获取学员登录日志
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        [ApiCheck, HttpPost]
        public ActionResult GetStudentAudits(string userId)
        {
            #region 参数验证解密

            if (userId.IsEmpty())
            {
                return Json(new BoolMessage(false, "请指定学员主键"));
            }

            var validResult = EncryptUtil.Decrypt(userId, "学员主键");
            if (!validResult.Success)
            {
                return Json(validResult);
            }

            userId = validResult.Data;

            #endregion

            #region 验证分页信息

            var pageInfo = GetPageInfo();
            if (!pageInfo.Success)
            {
                return Json(pageInfo);
            }

            #endregion

            var data = EduService.StudentAudits.GetPageList(pageInfo.Data, userId);
            return Json(new BoolMessagePageList<StudentAudits>(data));
        }

        /// <summary>
        /// 获取学员培训历史记录
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        [ApiCheck, HttpPost]
        public ActionResult GetTrainHistory(string userId)
        {
            #region 参数验证解密

            if (userId.IsEmpty())
            {
                return Json(new BoolMessage(false, "请指定学员主键"));
            }

            var validResult = EncryptUtil.Decrypt(userId, "学员主键");
            if (!validResult.Success)
            {
                return Json(validResult);
            }

            userId = validResult.Data;

            #endregion

            var data = EduService.Student.GetTrainHistoryList(userId);
            return Json(new BoolMessageData(data));
        }

        /// <summary>
        /// 获取学员考试记录
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        [ApiCheck, HttpPost]
        public ActionResult GetExamRecord(string userId)
        {
            #region 参数验证解密

            if (userId.IsEmpty())
            {
                return Json(new BoolMessage(false, "请指定学员主键"));
            }

            var validResult = EncryptUtil.Decrypt(userId, "学员主键");
            if (!validResult.Success)
            {
                return Json(validResult);
            }

            userId = validResult.Data;

            #endregion

            var studentIdNumber = EduService.Student.Get(userId).IDCardNo;
            var data = EduService.ExamRecord.GetList(studentIdNumber);
            return Json(new BoolMessageData(data));
        }

        /// <summary>
        /// 获取通知公告
        /// </summary>
        /// <returns></returns>
        public ActionResult GetNotice()
        {
            #region 验证分页信息

            var pageInfo = GetPageInfo();
            if (!pageInfo.Success)
            {
                return Json(pageInfo);
            }

            #endregion

            var data = EduService.Notice.GetPageList(pageInfo.Data, null, null, null);
            return Json(new BoolMessagePageList<Notice>(data));
        }

        public ActionResult GetNoticeDetails(string id)
        {
            #region 参数验证解密

            if (id.IsEmpty())
            {
                return Json(new BoolMessage(false, "请指定通知主键"));
            }

            var validResult = EncryptUtil.Decrypt(id, "通知主键");
            if (!validResult.Success)
            {
                return Json(validResult);
            }

            id = validResult.Data;

            #endregion

            var data = EduService.Notice.Get(id);
            return Json(new BoolMessageData(data));
        }
    }
}